﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using PhotgalleryEntities.Interfaces;
using PhotoGalleryEnteties;

namespace PhotogalleryModel
{
    public class AlbumRepository
    {
        public AlbumRepository (ModelContext modelContext)
        {
            _context = modelContext.Context;
        }

        private IContext _context;

        public Album CreateAlbum()
        {
            Album album = _context.CreateObject<Album>();
            album.CreationDate = DateTime.Now;

            return album;
        }

        public void DeleteAlbum(int albumId)
        {
            Album albumTodelete = _context.Albums.Where(p => p.AlbumId == albumId).FirstOrDefault();
            if (albumTodelete .ParentAlbum == null)
                throw new GalleryModelException("Root album cannot be deleted"); 
            
            _context.DeleteObject(albumTodelete);

        }

        public void DeleteAlbum(Album album)
        {
            if (album == null)
                throw new ArgumentNullException();
            if (album.ParentAlbum == null)
                throw new GalleryModelException("Root album cannot be deleted"); 
            _context.DeleteObject(album);
        }

        #region SelectMethods 
        
        public Album GetAlbumById(int albumId)
        {
            Album album = _context.Albums.Where(p => p.AlbumId == albumId).FirstOrDefault();
            if(album ==null)
            {
                throw new GalleryModelException( String.Format("Album with id {0} does not exist ", albumId));
            }
            return album;
        }

        public IEnumerable  <Album> GetAlbumsForUser(string loweredUserName)
        {
            return _context.Albums.Where(p => p.Owner.UserName.ToLower() == loweredUserName);
        }


        public ItemsPage< IAlbumItem>  GetPage(int albumId,int?onPage,int currentPage,out int totalPages)
        {
            List<IAlbumItem> albumItems = new List<IAlbumItem>();
            Expression<Func<Album, IAlbumItem>> exp = (p) => (IAlbumItem) p;
            
            //todo : fix dat crappy code 
            List<IAlbumItem> albums =
               _context.Albums.Where(p => p.ParentAlbum .AlbumId == albumId).ToList().ConvertAll(p => (IAlbumItem)p); 
            List<IAlbumItem> photos =
                _context.Photos.Where(p => p.Album.AlbumId == albumId).ToList().ConvertAll(p => (IAlbumItem) p);
            albumItems.AddRange(albums);
            albumItems.AddRange(photos);
            
            return albumItems.GetPage(onPage, currentPage, out totalPages );
        }


        

        #endregion 

       
        public ICollection< ValidationError  > ValidateAlbum (Album album)
        {
            List<ValidationError> errorList = new List<ValidationError>();
            //check Title 
            if(album.Title ==null)
            {
                errorList.Add(new ValidationError() { ErrorTag ="Title",ErrorMessage = "TitleRequired" });
            }

            //Album should have unique name in its parent album

            string[] albumNames =
                album.ParentAlbum.ChildAlbums.Where(p => p.Title == album.Title).Select(p => p.Title).ToArray();
            if(albumNames.Length >1)
            {
                errorList.Add(new ValidationError()
                                  {
                                      ErrorTag = "",
                                      ErrorMessage =
                                          string.Format("Album {0} already has album with title {1}",
                                                        album.ParentAlbum.Title, album.Title)
                                  });
            }

            return errorList;
        }
        
        
        public string CreateRootName(string UserName)
        {
            return UserName;
        }

    }
}
